package cn.csc.module.pay.controller.admin.card;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.common.pojo.CommonResult;
import static cn.csc.framework.common.pojo.CommonResult.success;

import cn.csc.framework.excel.core.util.ExcelUtils;

import cn.csc.framework.operatelog.core.annotations.OperateLog;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csc.module.pay.controller.admin.card.vo.*;
import cn.csc.module.pay.dal.dataobject.card.PayCardDO;
import cn.csc.module.pay.convert.card.PayCardConvert;
import cn.csc.module.pay.service.card.PayCardService;

@Api(tags = "管理后台 - 见证宝资金账户身份认证")
@RestController
@RequestMapping("/pay/card")
@Validated
public class PayCardController {

    @Resource
    private PayCardService cardService;

    @PostMapping("/create")
    @ApiOperation("创建见证宝资金账户身份认证")
    @PreAuthorize("@ss.hasPermission('pay:card:create')")
    public CommonResult<Long> createCard(@Valid @RequestBody PayCardCreateReqVO createReqVO) {
        return success(cardService.createCard(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新见证宝资金账户身份认证")
    @PreAuthorize("@ss.hasPermission('pay:card:update')")
    public CommonResult<Boolean> updateCard(@Valid @RequestBody PayCardUpdateReqVO updateReqVO) {
        cardService.updateCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除见证宝资金账户身份认证")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:card:delete')")
    public CommonResult<Boolean> deleteCard(@RequestParam("id") Long id) {
        cardService.deleteCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得见证宝资金账户身份认证")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:card:query')")
    public CommonResult<PayCardRespVO> getCard(@RequestParam("id") Long id) {
        PayCardDO card = cardService.getCard(id);
        return success(PayCardConvert.INSTANCE.convert(card));
    }

    @GetMapping("/list")
    @ApiOperation("获得见证宝资金账户身份认证列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('pay:card:query')")
    public CommonResult<List<PayCardRespVO>> getCardList(@RequestParam("ids") Collection<Long> ids) {
        List<PayCardDO> list = cardService.getCardList(ids);
        return success(PayCardConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得见证宝资金账户身份认证分页")
    @PreAuthorize("@ss.hasPermission('pay:card:query')")
    public CommonResult<PageResult<PayCardRespVO>> getCardPage(@Valid PayCardPageReqVO pageVO) {
        PageResult<PayCardDO> pageResult = cardService.getCardPage(pageVO);
        return success(PayCardConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出见证宝资金账户身份认证 Excel")
    @PreAuthorize("@ss.hasPermission('pay:card:export')")
    @OperateLog(type = EXPORT)
    public void exportCardExcel(@Valid PayCardExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayCardDO> list = cardService.getCardList(exportReqVO);
        // 导出 Excel
        List<PayCardExcelVO> datas = PayCardConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "见证宝资金账户身份认证.xls", "数据", PayCardExcelVO.class, datas);
    }

}
